<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2017/12/20
 * Time: 下午9:56
 */

namespace frontend\modules\v1\controllers;


use common\logic\HouseLogic;
use common\logic\BannerLogic;
use common\logic\MessageLogic;
use frontend\common\ApiController;
use common\logic\HouseTypeImglistLogic;
use common\logic\HouseTypeImageLogic;
use common\models\HouseTypeImageModel;
use common\models\HouseTypeImglistModel;

class HouseController extends ApiController
{

    /**
     * 房源列表
     * @return \yii\web\Response
     */
    public function actionList()
    {
        $page = $this->getPage();

        $pageSize = $this->getPageSize();
        $data = HouseLogic::instance()->getByPage($page, $pageSize,\Yii::$app->request->get());

        return $this->success($data);
    }

    /**
     * 房源推荐列表
     * @return \yii\web\Response
     */
    public function actionRecommendList()
    {
        $data = HouseLogic::instance()->getRecommend();

        return $this->success($data);
    }

    /**
     * 同区域的楼盘推荐
     * @return \yii\web\Response
     */
    public function actionGetSameRegionHouse()
    {
        $regionId = \Yii::$app->request->get('region_id');
        if(empty($regionId)){
            return $this->error('缺少参数');

        }
        $data = HouseLogic::instance()->getSameRegionHouse($regionId);

        return $this->success($data);
    }

    /**
     * 获取房源详情
     * @return \yii\web\Response
     */
    public function actionGet()
    {
        $id = \Yii::$app->request->get('id');
        $uid = \Yii::$app->request->get('uid');
        if(empty($id)){
            return $this->error('缺少参数');

        }
        $result = HouseLogic::instance()->getHouseDetail($id,$uid);
        if($result ===false){
            return $this->error('房源不存在');
        }
        return $this->success($result);

    }

    /**
     * 广告图片
     * @return \yii\web\Response
     */
    public function actionGetAdv(){
        return $this->success(HouseLogic::instance()->getAdv());
    }
	
	public function actionGetAdvCity(){
		$id = \Yii::$app->request->get('region');
		$res = BannerLogic::instance()->getAdv($id);
		
		if(count($res['banners'])<=0){
			return $this->success(HouseLogic::instance()->getAdv());
		}
		
        return $this->success(BannerLogic::instance()->getAdv($id));
    }

    /**
     * 房源动态
     * @return \yii\web\Response
     */
    public function actionMessage(){
        $id = \Yii::$app->request->get('id');
        $page = $this->getPage();

        $pageSize = $this->getPageSize();

        $data = MessageLogic::instance()->getByPage($id,$page, $pageSize);
        return $this->success($data);
    }

    public function actionAddpic(){
        // $models = HouseTypeImageLogic::instance()->All();
        // foreach ($models as $item) {
        //     $tmodels = new HouseTypeImglistModel();
        //     $tmodels->typeid = $item->id;
        //     $tmodels->img_path = $item->img_path;
        //     $tmodels->save();
        // }

        return $this->success(['ok'=>'ok']);
    }

}